package com.roc.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.roc.bean.AccountBalance;
import com.roc.dao.DaoException;
import com.roc.dao.IBaseDao;
import com.roc.dao.ISQLBaseDao;
import com.roc.pojo.Operators;
import com.roc.pojo.TblInsInf;
import com.roc.service.IAccountBalanceService;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.SysLogType;
@Service
@Transactional
public class AccountBalanceServiceImpl implements IAccountBalanceService {
	private static Logger logger=Logger.getLogger(AccountBalanceServiceImpl.class);
	@Autowired
	private IBaseDao baseDao;
	@Autowired
	private ISQLBaseDao sqlBaseDao;

	@Override
	public List<TblInsInf> loadOrgCode() throws ServiceException {
		List<TblInsInf> info=null;
		try {
			String sql="select s.whilte_num,s.whilte_name from apstfr.tbl_pos_whilte_list s where s.whilte_type='ins' and s.whilte_use=1 and s.remark1<>'1' ";
			List<Object> list=sqlBaseDao.queryAll(sql);
			if(list!=null && list.size()>0){
				info=new ArrayList<TblInsInf>();
				for(int i=0;i<list.size();i++){
					Object[] obj=(Object[]) list.get(i);
					TblInsInf tbInf=new TblInsInf();
					tbInf.setInsIdCd(Constance.Null2EmptyTrim(obj[0]));
					tbInf.setInsName(Constance.Null2EmptyTrim(obj[0])+"_"+Constance.Null2EmptyTrim(obj[1]));
					info.add(tbInf);
				}
			}			
		} catch (DaoException e) {
			logger.debug(e.getMessage(), e);
			throw new ServiceException("查询机构信息失败");
		}
		return info;
	}

	@Override
	public  Map<String, String> insertAccountBalanceData(AccountBalance ablance,Operators op)
			throws ServiceException {
		Map<String, String> info=new HashMap<String, String>();
		try {
			String insertSQL="insert into apstfr.tbl_ins_id_total_balance(today_date,ins_id_cd,account_id,current_amt,status) values(?,?,?,?,?)";
			AccountBalance record=queryAccountBalance(ablance);
			if(record!=null){
				String msg="日期["+ablance.getCurrentDate()+"]已存在机构号为["+ablance.getOrgCode()+"]的记录";
				logger.info(msg);
				info.put("result",msg);
				return info;
			}else{
				List<Object> paramsList=new ArrayList<Object>();
				paramsList.add(Constance.Null2EmptyTrim(ablance.getCurrentDate()));		
				paramsList.add(Constance.Null2EmptyTrim(ablance.getOrgCode()));
				paramsList.add(Constance.Null2EmptyTrim(ablance.getAccount()));
				paramsList.add(Double.parseDouble(Constance.Null2EmptyTrim(ablance.getAmt())));				
				paramsList.add("00");
				int effect=sqlBaseDao.deleteBySql(insertSQL, paramsList);
				if(effect>0){
					String msg="日期["+ablance.getCurrentDate()+"]机构号["+ablance.getOrgCode()+"]的记录添加成功";
					logger.info(msg);
					info.put("result",msg);
					info.put("flag","OK");
					baseDao.addSyslog(SysLogType.ACCOUNT_BALANCE_ADD.getCode(), SysLogType.ACCOUNT_BALANCE_ADD.getName(), op.getAccountNo(), msg);
					return info;
				}
			}
		} catch (DaoException e) {
			logger.debug(e.getMessage(), e);
			throw new ServiceException("添加账户余额记录失败");
		}
		return info;
	}

	@Override
	public Map<String, String> updateAccountBalanceData(AccountBalance ablance,Operators op)
			throws ServiceException {
		Map<String, String> info=new HashMap<String, String>();
		try {			
			String sql="update apstfr.tbl_ins_id_total_balance set account_id=?,current_amt=current_amt+?,status=? where today_date=? and ins_id_cd=? and account_id=? ";
			List<Object> paramsList=new ArrayList<Object>();			
			paramsList.add(Constance.Null2EmptyTrim(ablance.getAccount()));
			String lastestV=Constance.Null2EmptyTrim(ablance.getAmt());
			if("-".equals(ablance.getSymbol())){
				lastestV="-"+lastestV;
			}else if("+".equals(ablance.getSymbol())){
				//don't somethings			
			}else{
				String msg="修改符号错误";
				info.put("result",msg);
				return info;
			}
			paramsList.add(lastestV);
			paramsList.add(Constance.Null2EmptyTrim(ablance.getStatus()));
			paramsList.add(Constance.Null2EmptyTrim(ablance.getCurrentDate()));
			paramsList.add(Constance.Null2EmptyTrim(ablance.getOrgCode()));
			paramsList.add(Constance.Null2EmptyTrim(ablance.getAccount()));
			int row=sqlBaseDao.deleteBySql(sql, paramsList);
			if(row>0){
				String msg="日期["+ablance.getCurrentDate()+"]机构号["+ablance.getOrgCode()+"]的记录修改成功";
				logger.info(msg);
				info.put("result",msg);
				info.put("flag","OK");
				baseDao.addSyslog(SysLogType.ACCOUNT_BALANCE_UPDATE.getCode(), SysLogType.ACCOUNT_BALANCE_UPDATE.getName(), op.getAccountNo(), msg);
				return info;
			}
		} catch (DaoException e) {
			logger.debug(e.getMessage(), e);
			throw new ServiceException("修改账户余额记录失败");
		}
		
		return info;
	}
	/**
	 * 记录数
	 * @param ablance
	 * @return
	 * @throws ServiceException
	 */
	public AccountBalance queryAccountBalance(AccountBalance ablance)throws ServiceException{
		AccountBalance acctBal=null;
		try {
			String selSql="select today_date,ins_id_cd,account_id,current_amt,status from apstfr.tbl_ins_id_total_balance t where t.today_date=? and t.ins_id_cd=? and t.account_id=? ";
			Object[] parmas=new Object[3];
			parmas[0]=Constance.Null2EmptyTrim(ablance.getCurrentDate());
			parmas[1]=Constance.Null2EmptyTrim(ablance.getOrgCode());
			parmas[2]=Constance.Null2EmptyTrim(ablance.getAccount());
			Object object = sqlBaseDao.findObj(selSql, parmas);
			if(object!=null){
				Object[] obj = (Object[])object;
				acctBal=new AccountBalance();
				acctBal.setCurrentDate(Constance.Null2EmptyTrim(obj[0]));
				acctBal.setOrgCode(Constance.Null2EmptyTrim(obj[1]));
				acctBal.setAccount(Constance.Null2EmptyTrim(obj[2]));
				acctBal.setChangeValue(Constance.Null2EmptyTrim(obj[3]));
				acctBal.setAmt("0");
				acctBal.setStatus(Constance.Null2EmptyTrim(obj[4]));
				
			}			
		} catch (DaoException e) {
			logger.debug(e.getMessage(), e);
			throw new ServiceException("查询账户余额记录失败");
		}
		return acctBal;
	}
}
